﻿using System;
using System.AddIn.Hosting;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using p2pMessaging.Interfaces;

namespace p2pMessaging.Contracts
{
	public class MessageServiceStrategy
	{
		private readonly Collection<AddInToken> myTokens;
        private readonly Dictionary<Guid, IMessageService> myPlugins = new Dictionary<Guid, IMessageService>();
		
		public MessageServiceStrategy()
		{
			myTokens = AddInStore.FindAddIns(typeof(IMessageService), PipelineStoreLocation.ApplicationBase);

			foreach (AddInToken token in myTokens)
			{
				IMessageService service = token.Activate<IMessageService>(AddInSecurityLevel.Internet);
				myPlugins.Add(service.PluginId, service);
			}
		}
		
		public virtual void Send(IMessage message)
		{
			myPlugins[message.PluginId].Send(message);
		}
	}
}